/**
 * Created by zhangyanling.
 * 项目启动文件
 */
import React from 'react';
import ReactDOM from 'react-dom';
import { hashHistory } from 'react-router';
// Router
import RouterContainer from './router';
// Redux
import {Provider} from 'react-redux';
//axios
import axios from 'axios';
// Store
import configureStore from './store/configureStore';
// auth
import Auth from './auth/Auth'
//样式重置
import './reset.css';

axios.defaults.baseURL = 'http://192.168.1.2:7272';

// 添加请求拦截器
axios.interceptors.request.use(function (config) {
    let token = Auth.getToken();
    if(token){
        config.headers.Authorization  = 'Bearer ' + token
    }else{
        // window.location.href = 'http://localhost:8888/#/login'
        hashHistory.push('/login')
    }
    // 在发送请求之前做些什么
    return config;
}, function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
});

// 添加响应拦截器
axios.interceptors.response.use(function (response) {
    // 对响应数据做点什么
    // console.log(response)
    return response;
}, function (error) {
    // 对响应错误做点什么
    return Promise.reject(error);
});

// 注入store
const store = configureStore();
// 挂载
ReactDOM.render(
    <Provider store={store}>
        <RouterContainer />
    </Provider>, document.getElementById('app')
);